<template>
    <rx-dialog @ok="handTrans" @cancel="cancel">
        <rx-layout>
            <template #middle>
                <div class="x-bpm-content">
                    <a-form layout="vertical" class="rx-a-form-vertical" ref="form" :model="form" :rules="rules">
                        <div style="font-size: 16px;color: #4d9eff;display:inline-block;padding-bottom: 16px;">{{ form.subject }}</div>

                        <a-form-item :label="i18n('flowPage.transferor','流转人')"
                                     name="toUser">
                            <!--rx-input-button width="100%" v-model="form.toUser"
                                             @click="selectUsers"></rx-input-button-->
                            <rx-user-component v-model="form.toUser" :isAccount="false"
                                               :single="false"></rx-user-component>
                        </a-form-item>
                        <a-form-item :label="i18n('flowPage.approveType','审批类型')"
                                     name="approveType">
                            <a-radio-group :options="approveOptions" v-model:value="form.approveType">
                            </a-radio-group>
                        </a-form-item>

                        <a-form-item :label="i18n('flowPage.remarks','备注')"
                                     name="opinion">
                            <a-textarea v-model:value="form.opinion" placeholder="请输入..." style="width: 100%"/>
                        </a-form-item>


                        <template v-if="form.approveType=='parallel'">

                            <a-form-item :label="i18n('flowPage.completeType','完成配置')" name="completeType">
                                <a-radio-group :options="completeOptions" v-model:value="form.completeType"></a-radio-group>
                            </a-form-item>

                        </template>
                        <template v-if="form.completeType==1 && form.approveType=='parallel'">
                            <a-form-item :label="i18n('flowPage.judgeType','计算类型')" name="completeJudgeType">
                                <a-radio-group :options="completeJudgeOptions" v-model:value="form.completeJudgeType"></a-radio-group>
                            </a-form-item>
                        </template>
                        <template v-if="form.completeType==1 && form.approveType=='parallel'">
                            <a-form-item :label="i18n('flowPage.votes','投票数')" name="completeSetting">
                                <div style="display: flex;align-items: center;">
                                    <a-input-number
                                        v-model:value="form.completeSetting" :min="1"
                                        :max="form.completeJudgeType=='voteCount'?form.toUser.length:100"
                                        style="width:100px;margin-right: 6px;"
                                    />
                                    <span v-if="form.completeJudgeType=='voteCount'">{{ i18n('flowPage.ticket', '票') }}</span>
                                    <span v-else>%</span>
                                </div>

                            </a-form-item>
                        </template>
                    </a-form>
                </div>
            </template>
        </rx-layout>
    </rx-dialog>
</template>
<script>

import BpmPublicApi from '@/common/api/bpm/BpmPublicApi'
import {BaseFormModel, RxDialog, Util} from '@lowcode/jpaas-base-lib';
import {RxUserComponent} from '@lowcode/jpaas-share-component';
import BpmtaskApi from "@/common/api/bpm/bpmTask";

export default {
    name: 'BpmTaskRoamTransfer',
    props: {
        subject: String,
        taskId: String,
        layerid: String,
        destroy: Function
    },
    mixins: [BaseFormModel],
    components: {
        RxDialog,
        RxUserComponent
    },
    data() {
        return {
            rules: {
                toUser: [{
                    required: true,
                    message: this.i18n('flowPage.transferor', '请选择流转人', 'select'),
                    trigger: 'change'
                }]
            },
            msgOptions: [],
            transferOptions: [
                {label: this.i18n('flowPage.options.return_', '返回'), value: 'waitAllVoted'},
                {label: this.i18n('flowPage.options.next', '审批下一步'), value: 'jumpNext'}
            ],
            approveOptions: [
                {label: this.i18n('flowPage.options.serial', '串行'), value: 'sequential'},
                {label: this.i18n('flowPage.options.parallel', '并行'), value: 'parallel'}
            ],
            completeOptions: [
                {label: this.i18n('flowPage.options.default', '默认'), value: 0},
                {label: this.i18n('flowPage.options.advancedConf', '高级配置'), value: 1}
            ],
            completeJudgeOptions: [
                {label: this.i18n('flowPage.options.voteNumber', '票数'), value: 'voteCount'},
                {label: this.i18n('flowPage.options.percentage', '百分比'), value: 'votePercent'}
            ],
            form: {
                transferType: "waitAllVoted",
                approveType: "parallel",
                completeType: 0,
                completeJudgeType: "voteCount",
                completeSetting: 1,
                opinion: "",
                toUser: [],
                msgTypes: []
            },
            aryNames: [],
            aryIds: []
        }
    },
    created() {
        this.init();
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "bpmTask", key);
        },
        init() {
            this.form.subject = this.subject;
            this.form.taskId = this.taskId;

            BpmPublicApi.getMessageHandler().then(res => {
                res.forEach((val) => {
                    this.msgOptions.push({label: val.name, value: val.type})
                })
            })
        },

        handTrans(e) {
            var self_ = this;
            this.$refs.form.validate().then(() => {
                self_.form.msgType = self_.form.msgTypes.join(",");
                self_.$confirm({
                    title: self_.i18n('flowPage.confirmFlowTips', '确认流转任务吗?'),
                    cancelText: self_.$ti18('rxForm.cancelBtn', '取消'),
                    okText: self_.$ti18('rxForm.ackBtn', '确认'),
                    zIndex: 9999,
                    onOk() {
                        BpmtaskApi.transRoamTask(self_.form).then(res => {
                            e.loading = false;
                            if (res.success) {
                                Util.closeWindow(self_, "ok")
                            }
                        })
                    },
                    onCancel() {
                        e.loading = false;
                        return;
                    },
                });
            }).catch((error) => {
                e.loading = false;
            })
        }
    }
}
</script>
<style scoped>
.x-bpm-content ::v-deep(.ant-form-item-label){
    font-weight: bold;
}
</style>
